﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;

namespace EFong.View.HotelBooking
{
    public partial class Frm_HotelBookingDetailEdit : BasicForm
    {
        private Proxy.BaseServiceProxy _baseProxy {get{ return Proxy.BaseServiceProxy.GetInstance();}}

        public Frm_HotelBookingDetailEdit()
        {
            InitializeComponent();
            spe_BasePriceOrVendorDiscount.SetValue(null);
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.dte_CheckInDate.DateTimeChanged += (s, e) => { updateDaysField(); updateTotalFields(); updateIntegral(); };
            this.dte_CheckOutDate.DateTimeChanged += (s, e) => { updateDaysField(); updateTotalFields(); updateIntegral(); };

            this.spe_Rooms.ValueChanged += (s, e) => { updateTotalFields(); updateIntegral(); };
            this.spe_Price.ValueChanged += (s, e) => { updateTotalFields(); updateIntegral(); };
            this.spe_BasePriceOrVendorDiscount.ValueChanged += (s, e) => { updateTotalFields(); };
            this.spe_HotelTax.ValueChanged += (s, e) => { updateTotalFields(); };
            this.rad_BasePrice.CheckedChanged += (s, e) => { updateTotalFields(); };
            this.rad_VendorDiscount.CheckedChanged += (s, e) => { updateTotalFields(); };

            this.cmb_HotelLevel.SelectedIndexChanged += (s, e) => { updateIntegral(); updateIntegral(); };
        }

        protected override void LoadDatas()
        {
            this.cmb_HotelLevel.Properties.BindingData(_baseProxy.GetAllHotelLevel().ReturnValue);
        }

        private void updateDaysField()
        {
            Contract.HotelBookingDetail detail = getDetail();

            this.spe_Days.SetValue(detail.Days);
        }

        private void updateTotalFields()
        {
            Contract.HotelBookingDetail detail = getDetail();

            this.txt_TotalPrice.SetText(detail.TotalPrice);
            this.txt_TotalProfit.SetText(detail.Profit);
            this.txt_TotalVendorPrice.SetText(detail.TotalVendorPrice);

            
        }

        private void updateIntegral()
        {
            Contract.HotelBookingDetail detail = getDetail();

            if (detail.HotelBooking != null && detail.HotelBooking.Customer != null)
                spe_TotalIntegralExchange.SetValue(detail.CulcIntergral(detail.HotelBooking.Customer));
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Save":
                    this.save();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void save()
        {
            Contract.HotelBookingDetail detail = null;

            UIOperManager.SaveData(
                () =>
                {
                    return true;
                },
                () =>
                {
                    detail = getDetail();
                    updateToDetail(detail);
                    return true;
                },
                string.Empty,
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                });
        }

        private Contract.HotelBookingDetail _detail;
        public Contract.HotelBookingDetail Detail
        {
            get { return _detail; }
            set
            {
                _detail = value;
                onDetailChanged();
            }
        }

        private void onDetailChanged()
        {
            if (Detail == null)
            {

            }
            else
            {
                this.dte_CheckInDate.SetValue(Detail.CheckInDate);
                this.dte_CheckOutDate.SetValue(Detail.CheckOutDate);
                this.txt_GuestName.SetText(Detail.GuestName);
                this.txt_ConfirmationRef.SetText(Detail.HotelConfirmationRef);
                this.txt_HotelName.SetText(Detail.HotelName);
                this.txt_HotelOrderRef.SetText(Detail.HotelOrderRef);
                this.spe_HotelTax.SetValue(Detail.HotelTax);
                this.spe_Price.SetValue(Detail.Price);
                this.cmb_HotelLevel.SetDisplay(Detail.HotelLeave,"Name");
                this.spe_Rooms.SetValue(Detail.Rooms);
                this.txt_RoomType.SetText(Detail.RoomType);

                if (Detail.VendorDiscount.HasValue)
                {
                    this.rad_VendorDiscount.Checked = true;
                    this.spe_BasePriceOrVendorDiscount.SetValue(Detail.VendorDiscount);
                }
                else if (Detail.VendorPrice.HasValue)
                {
                    this.rad_BasePrice.Checked = true;
                    this.spe_BasePriceOrVendorDiscount.SetValue(Detail.VendorPrice);
                }
                else
                {
                    this.rad_BasePrice.Checked = false;
                    this.rad_VendorDiscount.Checked = true;
                    this.spe_BasePriceOrVendorDiscount.SetValue(null);
                }

                spe_TotalIntegralExchange.SetValue(Detail.Intergral);


                updateDaysField(); 
                updateTotalFields();
            }
        }

        private Contract.HotelBookingDetail getDetail()
        {
            Contract.HotelBookingDetail detail = new Contract.HotelBookingDetail();

            detail.CheckInDate = this.dte_CheckInDate.GetValue(AppParameters.FetureDateTime);
            detail.CheckOutDate = this.dte_CheckOutDate.GetValue(AppParameters.FetureDateTime);
            detail.GuestName = this.txt_GuestName.GetText();
            detail.HotelConfirmationRef = this.txt_ConfirmationRef.GetText();
            detail.HotelLeave = this.cmb_HotelLevel.GetDisplay();
            detail.HotelName = this.txt_HotelName.GetText();
            detail.HotelTax = this.spe_HotelTax.GetValue();
            detail.Price = this.spe_Price.GetValue();
            detail.Rooms = (int)this.spe_Rooms.GetValue();
            detail.RoomType = this.txt_RoomType.GetText();
            detail.HotelOrderRef = this.txt_HotelOrderRef.GetText();

            decimal? vendorDiscountOrBasePrice = this.spe_BasePriceOrVendorDiscount.GetValue();
            if (vendorDiscountOrBasePrice.HasValue && rad_VendorDiscount.Checked)
            {
                detail.VendorDiscount = vendorDiscountOrBasePrice;
                detail.VendorPrice = null;
            }
            else if (vendorDiscountOrBasePrice.HasValue && rad_BasePrice.Checked)
            {
                detail.VendorPrice = vendorDiscountOrBasePrice;
                detail.VendorDiscount = null;
            }
            else
            {
                detail.VendorPrice = detail.VendorDiscount = null;
            }

            detail.Intergral = spe_TotalIntegralExchange.GetValue();

            if (Detail != null)
                detail.HotelBooking = Detail.HotelBooking;

            return detail;
        }

        private void updateToDetail(Contract.HotelBookingDetail detail)
        {
            Detail.CheckInDate = detail.CheckInDate;
            Detail.CheckOutDate = detail.CheckOutDate;
            Detail.GuestName = detail.GuestName;
            Detail.HotelConfirmationRef = detail.HotelConfirmationRef;
            Detail.HotelLeave = detail.HotelLeave;
            Detail.HotelName = detail.HotelName;
            Detail.HotelTax = detail.HotelTax;
            Detail.Price = detail.Price;
            Detail.Rooms = detail.Rooms;
            Detail.RoomType = detail.RoomType;
            Detail.HotelOrderRef = detail.HotelOrderRef;
            Detail.VendorDiscount = detail.VendorDiscount;
            Detail.VendorPrice = detail.VendorPrice;
            Detail.Intergral = detail.Intergral;
        }


        protected override void OnViewStateChanged()
        {
            switch (ViewState)
            {
                case ViewStates.EDIT:
                    if (!AppParameters.IsAdRole)
                    {
                        spe_Price.ReadOnly(true);
                        rad_VendorDiscount.ReadOnly(true);
                        rad_BasePrice.ReadOnly(true);
                        spe_BasePriceOrVendorDiscount.ReadOnly(true);
                        spe_HotelTax.ReadOnly(true);
                        spe_Rooms.ReadOnly(true);
                        dte_CheckInDate.ReadOnly(true);
                        dte_CheckOutDate.ReadOnly(true);
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
